package com.sikaryofficial.backend.config;

import cn.hutool.core.text.CharSequenceUtil;
import com.google.common.collect.Lists;
import com.sikaryofficial.backend.constant.TenantConstant;
import com.sikaryofficial.backend.domain.vo.BrandTenantVO;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 33
 * 功能：多租户过滤配置信息
 * 时间：2023/11/30 20:20
 */

@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "mybatis-plus.tenant")
public class TenantFilterProperties {

    /**
     * 是否开启多租户 默认开启
     */
    private Boolean enable = true;

    /**
     * 租户id字段名
     */
    private String column = TenantConstant.TENANT_ID;

    /**
     * 需要进行租户过滤的表
     */
    private Set<String> filterTables;

    /**
     * 品牌与租户对应关系
     */
    private List<BrandTenantVO> brandTenantList;

    public List<Long> getTenantIdList() {
        List<Long> newIds = Lists.newArrayList();
        for(BrandTenantVO item : brandTenantList) {
            newIds.add(Long.valueOf(item.getTenantId()));
        }
        return newIds;
    }

    public BrandTenantVO getByBrand(String brandNo) {
        return brandTenantList.stream().filter(item -> CharSequenceUtil.equalsIgnoreCase(item.getBrandNo(), brandNo)).findFirst().orElse(new BrandTenantVO());
    }

    public String getBrandByTenantId(Integer tenantId) {
        return brandTenantList.stream().filter(item -> Objects.nonNull(tenantId)
                && item.getTenantId().equals(tenantId)).findFirst().orElse(new BrandTenantVO()).getBrandNo();
    }

    public String getTenantStrByBrand(String brandNo) {
        return String.valueOf(getTenantByBrand(brandNo));
    }

    public Integer getTenantByBrand(String brandNo) {
        return getByBrand(brandNo).getTenantId();
    }
}
